/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sbbs.base.web.primefaces;



import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.sbbs.base.dao.BaseDao;
import org.sbbs.base.web.search.PropertySearchBuilder;
import org.sbbs.genericdao.search.Filter;
import org.sbbs.genericdao.search.Search;
import org.sbbs.genericdao.search.SearchResult;
import org.sbbs.genericdao.search.Sort;

public class BaseLazyModel<T extends Object, ID extends Serializable> extends LazyDataModel<T> {

    private Map<String, Object> outFilters = new HashMap<String, Object>();
    private Search initSearch = null;
    private boolean initDisplay = true;
    private String searchSplitor;

    public String getSearchSplitor() {
        return searchSplitor;
    }

    public void setSearchSplitor(String searchSplitor) {
        this.searchSplitor = searchSplitor;
    }

    public boolean isInitDisplay() {
        return initDisplay;
    }

    public void setInitDisplay(boolean initDisplay) {
        this.initDisplay = initDisplay;
    }

    public Search getInitSearch() {
        return initSearch;
    }

    public void setInitSearch(Search initSearch) {
        this.initSearch = initSearch;
    }

    private boolean outFilter = false;

    public Map<String, Object> getOutFilters() {
        return outFilters;
    }

    public void setOutFilters(Map<String, Object> outFilters) {
        this.outFilters = outFilters;
    }

    public boolean isOutFilter() {
        return outFilter;
    }

    public void setOutFilter(boolean outFilter) {
        this.outFilter = outFilter;
    }

    private BaseDao dao;

    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    @Override
    public ID getRowKey(T obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    ;

//    public  T getRowData(String rowKey){return null;};
//     public T getRowData(String rowKey) 
    @Override
    public T getRowData(String id) {
        // return (T) this.dao.find(id);
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        Search search = new Search();

        //sort
        if (sortField != null) {
            search.addSort(sortField, SortOrder.ASCENDING.equals(sortOrder) ? true : false);
        }
        //filters
        if (outFilters != null && !outFilters.isEmpty()) {
            Filter filter = PropertySearchBuilder.builderSearchFilters(this.getOutFilters(), this.getSearchSplitor());
            search.addFilter(filter);
        } else if (!this.initDisplay) {

            Filter f = Filter.custom("1 = 2");;
            search.addFilter(f);
        }
        if (this.initSearch != null) {
            List<Filter> l = this.initSearch.getFilters();
            for (Iterator<Filter> itr = l.iterator(); itr.hasNext();) {
                Filter f = itr.next();
                search.addFilter(f);
            }

            List<Sort> s = this.initSearch.getSorts();
            for (Iterator<Sort> itr = s.iterator(); itr.hasNext();) {
                Sort f = itr.next();
                search.addSort(f);
            }
        }
        search.setFirstResult(first);
        search.setMaxResults(pageSize);
        SearchResult sr = this.dao.searchAndCount(search);
        this.setRowCount(sr.getTotalCount());
        return sr.getResult();

    }
//    public void onRowSelect(SelectEvent event){
//       throw new UnsupportedOperationException("onRowSelect(SelectEvent event) must be implemented when you want use select event.");
//  
//    }
//
//    public void onRowUnselect(UnselectEvent event) {
//             throw new UnsupportedOperationException("onRowUnselect(UnselectEvent event)  must be implemented when you want use unselect event.");
//    }
}
